package com.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.dao.BizOutStockDao;
import com.xq.entity.BizOutStock;
import com.xq.exception.BusinessException;
import com.xq.service.BizOutStockService;
import com.xq.vo.OutStockVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * (BizOutStock)表服务实现类
 *
 * @author makejava
 * @since 2020-08-09 17:03:47
 */
@Service("bizOutStockService")
public class BizOutStockServiceImpl extends ServiceImpl<BizOutStockDao, BizOutStock> implements BizOutStockService {
    /**
     *
     * @param current 当前页数
     * @param size 每页的最大展示数
     * @param outStockVo 分页查询条件
     * @return
     */
    @Override
    public Page<BizOutStock> findByPage(Integer current, Integer size, OutStockVo outStockVo) {
        //创建分页对象
        Page<BizOutStock> page=new Page<>(current,size);
        //创建自定义查询条件的对象
        QueryWrapper<BizOutStock> wrapper=new QueryWrapper<>();
        //自定义sql需要指定逻辑删除
        wrapper.eq("deleted",0);
        //判断条件对象是否为空 防止空指针异常
        if(!StringUtils.isEmpty(outStockVo)){
            //自定义条件
            if(!StringUtils.isEmpty(outStockVo.getOutNum())){
                wrapper.eq("out_num",outStockVo.getOutNum());
            }
            if(!StringUtils.isEmpty(outStockVo.getType())){
                wrapper.eq("type",outStockVo.getType());
            }
            if(!StringUtils.isEmpty(outStockVo.getStatus())){
                wrapper.eq("status",outStockVo.getStatus());
            }
        }
        return this.baseMapper.findByPage(page,wrapper);
    }

    /**
     * 订单回收操作
     *
     * @param status
     */
    @Override
    public Boolean updByStatus(Integer id,Integer status) {
        BizOutStock bk = this.baseMapper.selectById(id);
            bk.setStatus(status);
            return this.updateById(bk);
    }

    /**
     * 删除订单
     *
     * @param id
     * @return
     */
    @Override
    public Boolean removeByOutstock(Integer id) {
        int i = this.baseMapper.deleteById(id);
        if(i>0){
            return true;
        }else {
            throw new BusinessException(20001,"删除失败！");
        }
    }

    /**
     * 审核通过订单
     *
     * @param id
     * @return
     */
    @Override
    public Boolean updByPass(Integer id,Integer status) {
        BizOutStock bk = this.baseMapper.selectById(id);
        bk.setStatus(0);
        int i = this.baseMapper.updateById(bk);
        if(i>0){
            return true;
        }else {
            throw new BusinessException(20001,"审核失败！");
        }
    }
}